package com.framework.myandroid.base

import androidx.annotation.NonNull
import com.framework.myandroid.eventbus.EventBusHelper
import com.framework.myandroid.utils.Constant
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


abstract class BaseCallback(baseView: BaseView) : Callback<BaseJson?> {
    private lateinit var mBaseView: BaseView
    override fun onResponse(call: Call<BaseJson?>?, @NonNull response: Response<BaseJson?>) {
        mBaseView.dismissLoadingDialog()
        val baseJson: BaseJson? = response.body()
        EventBusHelper.handleBaseJson(baseJson)
        if (baseJson != null) {
            if (baseJson.code !== 0) {
                mBaseView.responseError(baseJson.code, baseJson.msg)
            } else {
                responseSuccess(baseJson)
            }
        } else {
            mBaseView.responseError(Constant.UNKNOW_ERROR_CODE, Constant.UNKNOW_ERROR_MESSAGE)
        }
    }

    override fun onFailure(call: Call<BaseJson?>?, t: Throwable) {
        mBaseView.dismissLoadingDialog()
        mBaseView.requestFailure(t)
    }

    abstract fun responseSuccess(baseJson: BaseJson?)

}